package com.igeek.boot.controller;

import com.igeek.boot.entity.User;
import com.igeek.boot.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @package com.igeek.boot.controller
 * @Description 测试druid数据源
 * @Author LittlePunk [296526219@qq.ccom]
 * @Version 2022.1
 * @Date 2023/12/11 13:36
 */
@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired(required = false)
    private JdbcTemplate jdbcTemplate;

    @GetMapping("count")
    public Long selectCount(){
        Long count = jdbcTemplate.queryForObject("select count(*) from user", Long.class);
        log.info("count:{}" , count);
        return count;
    }


    /**
     * 单表的crud
     */
    @GetMapping("/{id}")
    public User findOne(@PathVariable Integer id){
        User user = userService.findOne(id);
        log.info("user:{}",user);
        return user;
    }

    @GetMapping("list")
    public List<User> findAll(String query){
        return userService.findAll(query);
    }

    @PostMapping("add")
    public boolean add(@RequestBody User user){
        return userService.insert(user);
    }

    @PutMapping("update")
    public boolean update(@RequestBody User user){
        return userService.update(user);
    }

    @DeleteMapping("/{id}")
    public boolean del(@PathVariable("id") int id){
        return userService.delete(id);
    }

    @GetMapping("/findNameByOid/{oid}")
    public String findNameByOid(@PathVariable("oid")int oid){
        return userService.findNameByOid(oid);
    }
}
